/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database.tables;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import database.DBManager;
import database.objects.DOObstacle;
import java.net.UnknownHostException;
import java.util.List;

/**
 *
 * @author trivo
 */
public class DTObstacle {
    private DB m_db;
    public DTObstacle() {
        m_db = DBManager.getInstance().getDB();
    }
    
    public DBCollection getCollection() {
        DBCollection collection = m_db.getCollection("Obstacle");
        return collection;
    }
    
    public DOObstacle getObstacle(String id) {
        
        DOObstacle object = null;
        BasicDBObject dbObject = new BasicDBObject();
        dbObject.put(DOObstacle.KEY_ID, id);
        DBCursor cursor = getCollection().find(dbObject);
        while(cursor.hasNext()) {
            BasicDBObject result = (BasicDBObject)cursor.next();
            String name = result.getString(DOObstacle.KEY_NAME);
            String des = result.getString(DOObstacle.KEY_DES);
            String imgURL = result.getString(DOObstacle.KEY_IMG_URL);
            object = new DOObstacle(id, name, des, imgURL);
        }
        
        return object;
    }
    
    public List<DOObstacle> getListObstacle() {
        List<DOObstacle> retList = null;
        DBCursor cursor = getCollection().find();
        while(cursor.hasNext()) {
            BasicDBObject queryResult = (BasicDBObject)cursor.next();
            String id = queryResult.getString(DOObstacle.KEY_ID);
            String des = queryResult.getString(DOObstacle.KEY_DES);
            String imgURL = queryResult.getString(DOObstacle.KEY_IMG_URL);
            String name = queryResult.getString(DOObstacle.KEY_NAME);
            DOObstacle object = new DOObstacle(id, name, des, imgURL);
            retList.add(object);
        }
        return retList;
    }
    
    public void insertObstacle(DOObstacle object) {
        BasicDBObject dBObject = new BasicDBObject();
        dBObject.put(DOObstacle.KEY_ID, object.getID());
        dBObject.put(DOObstacle.KEY_NAME, object.name);
        dBObject.put(DOObstacle.KEY_DES, object.desc);
        dBObject.put(DOObstacle.KEY_IMG_URL, object.imageURL);
        getCollection().insert(dBObject);
    }
    
    public void updateObstacle(DOObstacle object) {
        BasicDBObject dBObject = new BasicDBObject();
        dBObject.put(DOObstacle.KEY_NAME, object.name);
        dBObject.put(DOObstacle.KEY_DES, object.desc);
        dBObject.put(DOObstacle.KEY_IMG_URL, object.imageURL);
        // Search query
        BasicDBObject searchQuery = new BasicDBObject().append(DOObstacle.KEY_ID, object.getID());
        getCollection().update(searchQuery, dBObject);
    }
    
    public void deleteObstacle(String id) {
        BasicDBObject dbObject = new BasicDBObject();
        dbObject.put("id", id);
        getCollection().remove(dbObject);
    }
    
    public void deleteAll() {
        DBCursor cursor = getCollection().find();
        while(cursor.hasNext()) {
            getCollection().remove(cursor.next());
        }
    }
}
